<?php

namespace app\admin\controller\business;

use app\common\controller\Backend;

/**
 * 优惠券领取
 */
class Coupon extends Backend
{
    protected $relationSearch = true;

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();

        $this->model = model('Coupon.Receive');
    }

    // 添加优惠券领取记录
    public function add($ids = null)
    {
        if ($this->request->isPost()) 
        {   
            $params = $this->request->post('row/a');
            $params['busid'] = $ids;
            $params['cid'] = $this->request->post('cid');
            $params['status'] = $this->request->post('status');
            $params['createtime'] =time();

            if ($params) 
            {
                $result = $this->model->validate("Common/Coupon/Receive")->save($params);

                if ($result === FALSE) 
                {
                    $this->error($this->model->getError());
                    exit;
                }else
                {
                    $this->success();
                    exit;
                }
            }
        }

        return $this->view->fetch();
    }

    // 编辑优惠券领取记录
    public function edit($ids = null)
    {
        $row = $this->model->find($ids);

        if (!$row) 
        {
            $this->error(__('No Results were found'));
            exit;
        }

        if ($this->request->isPost()) 
        {
            $params = $this->request->post("row/a");

            if ($params) 
            {
                
                $params['busid'] = $ids;
                $params['applyid'] = $this->auth->id;
                $params['applytime'] =time();

                $result = $this->model->validate("Common/Coupon/Receive")->isUpdate(true)->save($params);

                if ($result === FALSE) 
                {
                    $this->error($this->model->getError());
                    exit;
                }else
                {
                    $this->success();
                    exit;
                }
            }
        }

        $this->assign("row", $row);

        return $this->view->fetch();
    }

    // 删除优惠券领取记录
    public function del($ids = null)
    {
        $row = $this->model->select($ids);
        
        if(!$row)
        {
            $this->error(__('No Results were found'));
            exit;
        }

        $result = $this->model->destroy($ids);

        if ($result === FALSE) 
        {
            $this->error($this->model->getError());
            exit;
        }else
        {
            $this->success();
            exit;
        }

        
    }
}